package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.TeacherPost;
import com.example.kaogong.sys.mapper.TeacherPostMapper;
import com.example.kaogong.sys.pojo.vo.TeacherPostVo;
import com.example.kaogong.sys.service.ITeacherPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 教师职位 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@Service
public class TeacherPostServiceImpl extends ServiceImpl<TeacherPostMapper, TeacherPost> implements ITeacherPostService {

    @Autowired
    private TeacherPostMapper teacherPostMapper;

    /**
     * 获取所有教师职位信息
     * @return
     */
    @Override
    public RespBean<List<TeacherPostVo>> getAllTeacherPost() {
        try {
            List<TeacherPostVo> teacherPostVoList = teacherPostMapper.getAllTeacherPost();
            return RespBean.success(teacherPostVoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error();
    }

    /**
     * 新增一条教师职称
     * @param postName 职称
     * @return
     */
    @Override
    public RespBean addTeacherPost(String postName) {
        try {
            // 判断该职称是否已存在
            Integer post = teacherPostMapper.selectCount(new QueryWrapper<TeacherPost>().eq("post_name", postName));
            if (post >= 1)
                return RespBean.error(postName + "已存在");

            // 插入职称信息
            int insert = teacherPostMapper.insert(new TeacherPost(null, postName, false));
            if (insert >= 1)
                return RespBean.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error();
    }

    /**
     * 根据职称编号删除职称
     * @param postId 职称编号
     * @return
     */
    @Override
    public RespBean deleteTeacherPost(Integer postId) {
        try {
            int update = teacherPostMapper.update(new TeacherPost(null, null, true), new UpdateWrapper<TeacherPost>().eq("post_id", postId));
            if (update >= 1)
                return RespBean.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error();
    }
}
